package nadav.tasher.lightool.graphics.view;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;


public class Utils {
    static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "chenyu");

    public static Element getCoaster(int color, int radii, int padding){
        ShapeElement coaster = new ShapeElement();
        coaster.setRgbColor(RgbColor.fromArgbInt(color));
        coaster.setCornerRadius(radii);
        coaster.setBounds(padding, padding, padding+coaster.getWidth(), padding+coaster.getHeight());
        return coaster;
    }

    public static Element getCoaster(int color, int radii, int paddingX, int paddingY) {
        ShapeElement coaster = new ShapeElement();
        coaster.setRgbColor(RgbColor.fromArgbInt(color));
        coaster.setCornerRadius(radii);
        coaster.setBounds(paddingX, paddingY, paddingX+coaster.getWidth(), paddingY+coaster.getHeight());
        return coaster;
    }

    public static Element getCoaster(int color, int radii, int left, int right, int top, int bottom) {
        ShapeElement coaster = new ShapeElement();
        coaster.setRgbColor(RgbColor.fromArgbInt(color));
        coaster.setCornerRadius(radii);
        coaster.setBounds(left, top, right, bottom);
        return coaster;
    }

    public static Element getSizedCoaster(int color, int radii, int padding, int sizeX, int sizeY){
        float[] corners = new float[]{
                radii,
                radii,
                radii,
                radii,
                radii,
                radii,
                radii,
                radii
        };
        ShapeElement shapeElement=new ShapeElement();
        shapeElement.setShape(sizeY);
        shapeElement.setShape(sizeX);
        shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
        return shapeElement;
    }

    public static void measure(final Component component){
        final ComponentContainer.LayoutConfig layoutConfig = component.getLayoutConfig();
        final int horizontalMode;
        final int horizontalSize;

        switch (layoutConfig.width){
            case ComponentContainer.LayoutConfig.MATCH_PARENT:
                HiLog.info(TAG, "case 1" );
                horizontalMode = Component.EstimateSpec.PRECISE;
                if (component.getComponentParent() instanceof DirectionalLayout
                        &&((DirectionalLayout)component.getComponentParent()).getOrientation()==DirectionalLayout.VERTICAL){
                    int marginLeft = component.getLayoutConfig().getMarginLeft();
                    int marginRight = component.getLayoutConfig().getMarginRight();
                    Component componentParent = (Component) component.getComponentParent();
                    horizontalSize = componentParent.getEstimatedWidth() - marginLeft - marginRight;
                }else {
                    Component componentParent = (Component) component.getComponentParent();
                    horizontalSize = componentParent.getEstimatedWidth();
                }
                break;
            case ComponentContainer.LayoutConfig.MATCH_CONTENT:
                HiLog.info(TAG, "case 2" );
                horizontalMode = Component.EstimateSpec.UNCONSTRAINT;
                horizontalSize=0;
                break;
            default:
                HiLog.info(TAG, "case default 1" );
                horizontalMode = Component.EstimateSpec.PRECISE;
                horizontalSize = layoutConfig.width;
                break;
        }
        HiLog.info(TAG, "horizontalSize = " +horizontalSize + " horizontalMode = "+ horizontalMode);
        final int horizontalMeasureSpec = Component.EstimateSpec
                .getSizeWithMode(horizontalSize,horizontalMode);

        final int verticalMode;
        final int verticalSize;
        switch (layoutConfig.height){
            case ComponentContainer.LayoutConfig.MATCH_PARENT:
                HiLog.info(TAG, "case 11" );
                verticalMode = Component.EstimateSpec.PRECISE;
                if (component.getComponentParent() instanceof DirectionalLayout
                        &&((DirectionalLayout)component.getComponentParent()).getOrientation() == DirectionalLayout.HORIZONTAL){
                    int marginTop = component.getLayoutConfig().getMarginTop();
                    int marginBottom = component.getLayoutConfig().getMarginBottom();
                    Component componentParent = (Component) component.getComponentParent();
                    verticalSize = componentParent.getEstimatedHeight() - marginBottom - marginTop;
                }else {
                    Component componentParent =(Component) component.getComponentParent();
                    verticalSize = componentParent.getEstimatedHeight();
                }
                break;
            case ComponentContainer.LayoutConfig.MATCH_CONTENT:
                HiLog.info(TAG, "case 22" );
                verticalMode = Component.EstimateSpec.UNCONSTRAINT;
                verticalSize = 0;
                break;
            default:
                HiLog.info(TAG, "case default 2" );
                verticalMode = Component.EstimateSpec.PRECISE;
                verticalSize = layoutConfig.height;
                break;
        }
        HiLog.info(TAG, "verticalSize = " +verticalSize + " verticalMode = "+ verticalMode);
        final int verticalMeasureSpec = Component.EstimateSpec.getSizeWithMode(verticalSize,verticalMode);
        component.estimateSize(horizontalMeasureSpec, verticalMeasureSpec);
    }
}
